#include<cstdlib>
#include "swf.h"
#include "TextRecord.h"
#include "RGBA.h"
using namespace swfstream;
TextRecord* TextRecord::from_file(FILE* f, bool in_define_text2, int GlyphBits,
		int AdvanceBits) {
//	puts("reading text_record");
	TextRecord *r = new TextRecord;
	byte tmp = fgetc(f);
//	printf("first byte:0x%X\n",(uint)tmp);
	if ((tmp & 0xf0) != 0x80) {
		//fprintf(stderr,"Reserved must be set to 1000B\n");
		ungetc(tmp, f);
		return NULL;
	}
	r->is_rgba = in_define_text2;
	r->StyleFlagsHasFont = ((tmp & 0x08) != 0);
	r->StyleFlagsHasColor = ((tmp & 0x04) != 0);
	r->StyleFlagsHasYOffset = ((tmp & 0x02) != 0);
	r->StyleFlagsHasXOffset = ((tmp & 0x01) != 0);
//	printf("StyleFlagsHasFont:%u,StyleFlagsHasColor:%u,StyleFlagsHasYOffset:%u,StyleFlagsHasXOffset:%u\n",(unsigned)r->StyleFlagsHasFont,(unsigned)r->StyleFlagsHasColor,(unsigned)r->StyleFlagsHasYOffset,(unsigned)r->StyleFlagsHasXOffset);
	if (r->StyleFlagsHasFont) {
		fread(&r->FontID, 2, 1, f);
//		printf("FontID:%u,",(uint)r->FontID);
	}
	if (r->StyleFlagsHasColor) {
		if (in_define_text2) {
			r->TextColor = RGBA::from_file(f); //->print();
		} else {
			r->TextColor = RGB::from_file(f); //->print();
		}
	}
	if (r->StyleFlagsHasXOffset) {
		fread(&r->XOffset, 2, 1, f);
//		printf("xoff=%d,",(int)r->XOffset);
	}
	if (r->StyleFlagsHasYOffset) {
		fread(&r->YOffset, 2, 1, f);
//		printf("yoff=%d,",(int)r->YOffset);
	}
	if (r->StyleFlagsHasFont) {
		fread(&r->TextHeight, 2, 1, f);
//		printf("textheight=%u,",(unsigned)r->TextHeight);
	}
	r->GlyphCount = getc(f);
//	printf("GlyphCount=%u\n",(unsigned)r->GlyphCount);
	r->GlyphEntries = GlyphEntry::list_from_file(f, GlyphBits, AdvanceBits,
			r->GlyphCount);
	return r;
}
std::vector<TextRecord*>* TextRecord::list_from_file(FILE* f,
		bool in_define_text2, int GlyphBits, int AdvanceBits) {
	//puts("reading text_record list");
	std::vector<TextRecord*> *r = new std::vector<TextRecord*>();
	TextRecord*t;
	while ((t = TextRecord::from_file(f, in_define_text2, GlyphBits,
			AdvanceBits)) != NULL) {
		r->push_back(t);
	}
	//printf("!!!!!!!!!!!!!!!!!!!!!!r=%p\n",r);
	//fgetc(f);
	return r;
}
void TextRecord::print() {
	printf(
			"text_record::StyleFlagsHasFont:%u,StyleFlagsHasColor:%u,StyleFlagsHasYOffset:%u,StyleFlagsHasXOffset:%u,",
			(uint) StyleFlagsHasFont, (uint) StyleFlagsHasColor,
			(uint) StyleFlagsHasYOffset, (uint) StyleFlagsHasXOffset);
	if (StyleFlagsHasFont) {
		printf("FontID:%u,", (uint) FontID);
	}
	if (StyleFlagsHasColor) {
		//			printf("!!!!!!%p!!!!!!",TextColor);
		if (is_rgba) {
			//				((RGBA*)TextColor)->print();
		} else {
			//				((RGB*)TextColor)->print();
		}
	}
	if (StyleFlagsHasXOffset) {
		printf("XOffset:%d,", (int) XOffset);
	}
	if (StyleFlagsHasYOffset) {
		printf("YOffset:%d,", (int) YOffset);
	}
	if (StyleFlagsHasFont) {
		printf("TextHeight:%u,GlyphCount:%u", (uint) TextHeight,
				(uint) GlyphCount);
	}
	putchar('\n');
	for (uint i = 0; i < GlyphCount; i++) {
		//GlyphEntries->at(i)->print();
	}
	//putchar('\n');
	//std::vector<GLYPHENTRY*>* GlyphEntries;///< GLYPHENTRY[GlyphCount] Glyph entry (see following).
}

TiXmlElement* TextRecord::toTiXmlElement() {
	TiXmlElement* r = new TiXmlElement("TextRecord");
	//r->SetAttribute("StyleFlagsHasFont", StyleFlagsHasFont);
	//r->SetAttribute("StyleFlagsHasColor", StyleFlagsHasColor);
	//r->SetAttribute("StyleFlagsHasXOffset", StyleFlagsHasXOffset);
	//r->SetAttribute("StyleFlagsHasYOffset", StyleFlagsHasYOffset);
	r->SetAttribute("GlyphCount", GlyphCount);
	if (StyleFlagsHasFont) {
		r->SetAttribute("fontId", FontID);
		r->SetAttribute("TextHeight", TextHeight);
	}
	if (StyleFlagsHasXOffset) {
		r->SetAttribute("XOffset", XOffset);
	}
	if (StyleFlagsHasYOffset) {
		r->SetAttribute("YOffset", YOffset);
	}
	if (StyleFlagsHasColor) {
		TiXmlElement* tmp = new TiXmlElement("TextColor");
		tmp->LinkEndChild(TextColor->toTiXmlElement());
		r->LinkEndChild(tmp);
	}
	TiXmlElement* tmp = new TiXmlElement("GlyphEntries");
	for (int i = 0; i < GlyphEntries->size(); i++) {
		tmp->LinkEndChild(GlyphEntries->at(i)->toTiXmlElement());
	}
	r->LinkEndChild(tmp);
	return r;
}
